<template>
  <div class="show-main">
    <el-scrollbar style="height:100%">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" size="small">
        <el-form-item>
          <el-input v-model="searchForm.name" placeholder="请输入平台方名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.stop="search">搜索</el-button>
          <el-button type="primary" @click.stop="reload">刷新</el-button>
          <el-button type="primary" @click.stop="add">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="table" border size="small">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="account" label="账号"></el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="卡号" prop="bank_no"></el-table-column>
        <el-table-column label="开户行" prop="bank_name"></el-table-column>
        <el-table-column label="支持划账">
          <template slot-scope="scope">
            {{scope.row.is_depend == 0 ?'不支持': '支持'}}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            {{statusName(scope.row.status)}}
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间">
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="changepwd(scope.row.id)">修改密码</el-button>
            <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="searchForm.page" :page-sizes="[10,20,30,40]" :page-size="searchForm.limit"
          layout="total, sizes, prev, pager, next, jumper" :total="count">
        </el-pagination>
      </div>
      <el-dialog title="添加平台方" :visible.sync="addVisible" width="500px" :before-close="handleClose('addForm')">
        <el-form ref="addForm" :model="addForm" label-width="80px" size="small" :rules="rules">
          <el-form-item label="账号" prop="account">
            <el-input v-model="addForm.account"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="addForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="addForm.name"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pwd">
            <el-input v-model="addForm.pwd"></el-input>
          </el-form-item>
          <el-form-item label="再次密码" prop="repwd">
            <el-input v-model="addForm.repwd"></el-input>
          </el-form-item>
          <el-form-item label="银行卡号" prop="bank_no">
            <el-input v-model="addForm.bank_no"></el-input>
          </el-form-item>
          <el-form-item label="开户行" prop="bank_name">
            <el-input v-model="addForm.bank_name"></el-input>
          </el-form-item>
          <el-form-item label="支持划账" prop="is_depend">
            <el-radio-group v-model="addForm.is_depend">
              <el-radio :label="0">不支持</el-radio>
              <el-radio :label="1">支持</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('addVisible', 'addForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('addVisible','addForm')" size="small">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="编辑平台方" :visible.sync="editVisible" width="500px" :before-close="handleClose('editForm')">
        <el-form ref="editForm" :model="editForm" label-width="80px" size="small" :rules="rules">
          <el-form-item label="账号" prop="account">
            <el-input v-model="editForm.account"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="editForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="editForm.name"></el-input>
          </el-form-item>
          <el-form-item label="银行卡号" prop="bank_no">
            <el-input v-model="editForm.bank_no"></el-input>
          </el-form-item>
          <el-form-item label="开户行" prop="bank_name">
            <el-input v-model="editForm.bank_name"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="editForm.status">
              <el-radio :label="0">禁用</el-radio>
              <el-radio :label="1">启用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="支持划账" prop="is_depend">
            <el-radio-group v-model="editForm.is_depend">
              <el-radio :label="0">不支持</el-radio>
              <el-radio :label="1">支持</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('editVisible', 'editForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('editVisible','editForm')" size="small">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="修改密码" :visible.sync="changeVisible" width="500px" :before-close="handleClose('changeForm')">
        <el-form ref="changeForm" :model="changeForm" label-width="80px" size="small" :rules="rules">
          <el-form-item label="密码" prop="pwd">
            <el-input v-model="changeForm.pwd"></el-input>
          </el-form-item>
          <el-form-item label="再次密码" prop="chkpwd">
            <el-input v-model="changeForm.chkpwd"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('changeVisible', 'changeForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('changeVisible','changeForm')" size="small">确 定</el-button>
        </span>
      </el-dialog>
    </el-scrollbar>
  </div>
</template>
<script>
import { platList, platOnly, platDelete, platAdd, platEdit, platChangePwd } from '@/network/api.js'

export default {
  name: 'plat',
  data () {
    return {
      searchForm: {
        page: 1,
        limit: 10,
        name: ''
      },
      count: 0, // 总记录数
      table: [],
      addForm: {
        account: '', // 账号
        pwd: '', // 密码
        name: '', // 名称
        repwd: '', // 再次输入密码
        phone: '', // 电话号码
        bank_no: '', // 银行卡号
        bank_name: '', // 开户行
        is_depend: 0 // 是否支持划账
      }, // 添加的表单
      editForm: {
        id: 0,
        account: '', // 账号
        name: '', // 名称
        phone: '', // 电话号码
        bank_no: '', // 银行卡号
        bank_name: '', // 开户行
        status: 0, // 状态
        is_depend: 0 // 是否支持划账
      }, // 编辑的表单
      changeForm: {
        id: 0,
        pwd: '',
        chkpwd: ''
      }, // 修改密码表单
      addVisible: false, // 添加表单
      editVisible: false, // 编辑表单
      changeVisible: false, // 密码修改表单
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        bank_no: [
          { required: true, message: '请输入银行卡号', trigger: 'blur' }
        ],
        bank_name: [
          { required: true, message: '请输入开户行', trigger: 'blur' }
        ],
        repwd: [
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请再次输入密码'))
              } else if (value !== this.addForm.pwd) {
                callback(new Error('两次输入密码不一致!'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        chkpwd: [
          {
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请再次输入密码'))
              } else if (value !== this.changeForm.pwd) {
                callback(new Error('两次输入密码不一致!'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.search()
  },
  computed: {
    statusName () {
      return (status) => {
        switch (status) {
          case 0: return '禁用'
          case 1: return '启用'
        }
      }
    }
  },
  methods: {
    reload () {
      location.reload()
    },
    async search () {
      // 获取平台列表
      try {
        const rtn = await platList(this.searchForm)
        this.table = rtn.data.list
        this.count = rtn.data.count
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 每页条数变化
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.search()
    },
    // 当前页变化
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.search()
    },
    // 添加管理员
    add () {
      this.addVisible = true
    },
    // 编辑管理员
    async edit (id) {
      try {
        const rtn = await platOnly({ id })
        this.editVisible = true
        this.$nextTick(() => {
          this.editForm = rtn.data
        })
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 修改密码
    changepwd (id) {
      this.changeForm.id = id
      this.changeVisible = true
    },
    // 删除
    async del (id) {
      try {
        await platDelete({ id })
        this.search()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 关闭弹出前操作(不包括取消按钮)
    handleClose (formName) {
      return (done) => {
        this.$refs[formName].resetFields()
        done()
      }
    },
    // 取消按钮
    close (visibleName, formName) {
      this.$refs[formName].resetFields()
      this[visibleName] = false
    },
    send (visibleName, formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          switch (formName) {
            case 'addForm': // 添加表单
              try {
                await platAdd(this.addForm)
                this.close('addVisible', 'addForm')
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
            case 'editForm': // 编辑表单
              try {
                await platEdit(this.editForm)
                this.close('editVisible', 'editForm')
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
            case 'changeForm': // 修改密码表单
              try {
                await platChangePwd(this.changeForm)
                this.close('changeVisible', 'changeForm')
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
